package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.MyDynamicStateAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ActivityAPI;
import com.runen.infinitemovement.model.entity.CompanyCircleEntity;
import com.runen.infinitemovement.model.entity.EventBusEntity;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CompanyCricleActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.rv_company_circle)
    RecyclerView rvCompanyCircle;
    @BindView(R.id.top_tv_right)
    TextView topTvRight;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    @BindView(R.id.ll_publish)
    LinearLayout llPublish;
    private MyDynamicStateAdapter companyCircleAdapter;
    private int page = 2;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_company_cricle;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager4 = new LinearLayoutManager(this);
        layoutManager4.setOrientation(RecyclerView.VERTICAL);
        rvCompanyCircle.setLayoutManager(layoutManager4);
        companyCircleAdapter = new MyDynamicStateAdapter(this, 0);
        companyCircleAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvCompanyCircle.setAdapter(companyCircleAdapter);
        companyCircleAdapter.getLoadMoreModule().setAutoLoadMore(false);
        initData();
        iniLoadMore();
    }

    private void iniLoadMore() {
        companyCircleAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMore();
            }
        });
        companyCircleAdapter.getLoadMoreModule().setAutoLoadMore(true);
        companyCircleAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    private void initData() {
        srl.setRefreshing(true);
        String str = "?limit=10&page=1";
        new ActivityAPI().getCompanyCircle(str, new BaseResultCallback<CompanyCircleEntity>() {
            @Override
            public void onResponse(CompanyCircleEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        companyCircleAdapter.setNewData(response.data);
                        if (response.data.size() >= response.count) {
                            companyCircleAdapter.getLoadMoreModule().loadMoreEnd(true);
                            companyCircleAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }

            }

            @Override
            public void onError(Throwable e, String body) {
                super.onError(e, body);
                srl.setRefreshing(false);
            }
        });
    }

    @OnClick({R.id.top_btn_left, R.id.ll_publish, R.id.top_tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.ll_publish:
                Intent intent = new Intent(this, PublishStatusActivity.class);
                intent.putExtra("PUBLISH_FLAG", 0);
                startActivity(intent);
                break;
            case R.id.top_tv_right:
                Intent intent7 = new Intent(this, MyDynamicStateActivity.class);
                startActivity(intent7);
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    public void loadMore() {
        String str = "?limit=10&page=%s";
        new ActivityAPI().getCompanyCircle(String.format(str, page), new BaseResultCallback<CompanyCircleEntity>() {
            @Override
            public void onResponse(CompanyCircleEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        companyCircleAdapter.addData(response.data);
                        if (companyCircleAdapter.getData().size() >= response.count) {
                            companyCircleAdapter.getLoadMoreModule().loadMoreEnd(true);
                            companyCircleAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            companyCircleAdapter.getLoadMoreModule().loadMoreComplete();
                            companyCircleAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventBusEntity event) {
        switch (event.getMsg()) {
            case 8:
                onRefresh();
                break;
            default:
                break;
        }
    }
}
